package com.logistic.server.controller;

import com.logistic.server.common.annotation.LoginRequired;
import com.logistic.server.entity.common.Result;
import com.logistic.server.entity.common.ResultGenerator;
import com.logistic.server.entity.common.TreeSelect;
import com.logistic.server.entity.common.WebUserToken;
import com.logistic.server.entity.pojo.SysMenu;
import com.logistic.server.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单权限 前端控制器
 * </p>
 *
 * @author swen
 * @since 2021-10-25
 */
@Api(value = "菜单权限接口", tags = {"菜单权限接口"})
@Slf4j
@LoginRequired
@RestController
@RequestMapping("/system/menu")
public class SysMenuController {

    @Resource
    private ISysMenuService menuService;

    /**
    * 菜单权限列表
    * @param menu 菜单权限
    * @param webUserToken 用户信息
    * @return Result
    */
    @ApiOperation(value = "菜单权限列表", notes = "菜单权限列表")
    @GetMapping
    public Result<?> list(SysMenu menu, WebUserToken webUserToken) {
        return ResultGenerator.genSuccessResult(menuService.selectMenuList(menu, webUserToken.getUserId()));
    }

    /**
    * 菜单权限新增
    * @param menu 菜单权限
    * @return Result
    */
    @ApiOperation(value = "菜单权限新增", notes = "菜单权限新增")
    @PostMapping
    public Result<?> add(@RequestBody SysMenu menu) {
        menuService.insertMenu(menu);
        return ResultGenerator.genSuccessResult();
    }

    /**
    * 菜单权限删除
    * @param menuId 菜单权限主键
    * @return Result
    */
    @ApiOperation(value = "菜单权限删除", notes = "菜单权限删除")
    @DeleteMapping("/{menuId}")
    public Result<?> delete(@PathVariable("menuId") Long menuId) {
        menuService.deleteMenuById(menuId);
        return ResultGenerator.genSuccessResult();
    }

    /**
    * 菜单权限修改
    * @param menu 菜单权限
    * @return Result
    */
    @ApiOperation(value = "菜单权限修改", notes = "菜单权限修改")
    @PutMapping
    public Result<?> update(@RequestBody SysMenu menu) {
        menuService.updateMenu(menu);
        return ResultGenerator.genSuccessResult();
    }

    /**
    * 菜单权限详情
    * @param id 菜单权限主键
    * @return Result
    */
    @ApiOperation(value = "菜单权限详情", notes = "菜单权限详情")
    @GetMapping("/{id}")
    public Result<?> detail(@PathVariable String id) {
        return ResultGenerator.genSuccessResult(menuService.getById(id));
    }

    /**
     * 获取菜单下拉树列表
     * @param menu 菜单
     * @param webUserToken 用户信息
     * @return 结果
     */
    @ApiOperation("获取菜单下拉树列表")
    @GetMapping("/treeSelect")
    public Result<List<TreeSelect>> treeSelect(SysMenu menu, WebUserToken webUserToken) {
        return ResultGenerator.genSuccessResult(menuService.treeSelect(menu, webUserToken.getUserId()));
    }

    /**
     * 加载对应角色菜单列表树
     * @param roleId 角色id
     * @param webUserToken 用户信息
     * @return 结果
     */
    @ApiOperation("加载对应角色菜单列表树")
    @GetMapping(value = "/roleMenuTreeSelect/{roleId}")
    public Result<Map<String, Object>> roleMenuTreeSelect(@PathVariable("roleId") Long roleId, WebUserToken webUserToken) {
        List<SysMenu> menus = menuService.selectMenuList(webUserToken.getUserId());
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", menuService.buildMenuTreeSelect(menus));
        return ResultGenerator.genSuccessResult(ajax);
    }
}
